define(function (require, exports, module) {
    require("apps/easyui");

    function Panel(options) {
        var that = this;
        this.panel = $("<div></div>").appendTo("body").dialog({
            title: "数据导入",
            width: 800,
            height: 500,
            closed: true,
            closable: true,
            href: WE.contextPath + "/app/api/dataImport/importPanel?keyCode=" + options.keyCode + "&uuid=" + $.utils.createUUID(6),
            buttons: [
                {
                    text: "关闭",
                    handler: function () {
                        that.panel.dialog("close")
                    }}
            ]
        });
        return this;
    }

    Panel.prototype.open = function () {
        this.panel.dialog("open");
    };

    exports.createImportPanel = function (options) {
        return new Panel($.extend({}, options));
    };

    function ImportProcess(options) {
        this.options = options;
        this.complete = true;
        this.processPanel = options.processPanel;
        this.processPanel.progressbar({value: 0, onChange: function (value) {
//            that.children().css({width:value + "%"});
        }});
    }

    ImportProcess.prototype.start = function () {
        if (!this.complete) {
            return;
        }
        this.complete = false;
        this.processPanel.progressbar("setValue", 0);
        var that = this;

        function start() {
            setTimeout(function () {
                var value = that.processPanel.progressbar('getValue');
                if (!that.complete && value < 99) {
                    value += 1;
                    that.processPanel.progressbar("setValue", value);
                }
                start();
            }, 1000)
        }

        start();
        if (this.options.onWhenStart) {
            this.options.onWhenStart.call(this);
        }
        $.post(WE.contextPath + "/app/api/dataImport/doImport", $.extend({}, this.options.postData, {
            keyCode: that.options.keyCode,
            filePath: that.options.filePath,
            fileName: that.options.fileName
        }), function (data) {
            that.end(data);
            if (that.options.onWhenEnd) {
                that.options.onWhenEnd.call(that);
            }
        }, "JSON")
    };
    ImportProcess.prototype.end = function (data) {
        this.processPanel.progressbar("setValue", 100);
        this.complete = true;
//        this.tipPanel.html('导入成功，共导入数据' + data.totalNum + "条，成功" + (data.totalNum - data.failureNum) + "条");

    };
    var pro = null;
    exports.doUpload = function (options) {
        pro = pro ? pro : new ImportProcess($.extend({}, options));
        pro.start();
        return pro;
    };
});
